# ============================================================================
# 平台特定配置模块
# ============================================================================
# 提供不同平台（Windows/Linux）的特定配置
# ============================================================================

# 初始化变量
set(INCLUDE_DIR "")
set(LIB_DIR "")
set(LIB_LINK_DEP "")
set(LIB_LINK_NF "")

if(UNIX)
    # ========================================================================
    # Linux 平台配置
    # ========================================================================
    
    # 检测 Linux 发行版
    set(DISTRO_ID "unknown")
    set(DISTRO_VERSION_ID "")
    
    if(${CMAKE_VERSION} VERSION_LESS "3.22.0")
        # 对于较老的 CMake 版本，使用文件检测方法
        message(STATUS "使用文件检测方法判断 Linux 发行版...")
        
        # 方法1：检查 /etc/os-release（现代 Linux 标准）
        if(EXISTS "/etc/os-release")
            file(READ "/etc/os-release" OS_RELEASE_CONTENT)
            
            # 提取 ID 字段
            string(REGEX MATCH "ID=\"?([^\"\n]*)\"?" ID_MATCH "${OS_RELEASE_CONTENT}")
            if(ID_MATCH)
                string(REGEX REPLACE "ID=\"?([^\"]*)\"?" "\\1" DISTRO_ID "${ID_MATCH}")
                string(TOLOWER "${DISTRO_ID}" DISTRO_ID)
            endif()
            
            # 提取 VERSION_ID 字段
            string(REGEX MATCH "VERSION_ID=\"?([^\"\n]*)\"?" VERSION_MATCH "${OS_RELEASE_CONTENT}")
            if(VERSION_MATCH)
                string(REGEX REPLACE "VERSION_ID=\"?([^\"]*)\"?" "\\1" DISTRO_VERSION_ID "${VERSION_MATCH}")
            endif()
            
        # 方法2：检查 /etc/redhat-release（CentOS/RHEL）
        elseif(EXISTS "/etc/redhat-release")
            file(READ "/etc/redhat-release" REDHAT_RELEASE_CONTENT)
            if(REDHAT_RELEASE_CONTENT MATCHES "CentOS")
                set(DISTRO_ID "centos")
            elseif(REDHAT_RELEASE_CONTENT MATCHES "Red Hat")
                set(DISTRO_ID "rhel")
            endif()
            
            # 提取版本号
            string(REGEX MATCH "([0-9]+)\\." VERSION_MATCH "${REDHAT_RELEASE_CONTENT}")
            if(VERSION_MATCH)
                string(REGEX REPLACE "([0-9]+)\\." "\\1" DISTRO_VERSION_ID "${VERSION_MATCH}")
            endif()
            
        # 方法3：检查 /etc/lsb-release（Ubuntu）
        elseif(EXISTS "/etc/lsb-release")
            file(READ "/etc/lsb-release" LSB_RELEASE_CONTENT)
            if(LSB_RELEASE_CONTENT MATCHES "Ubuntu")
                set(DISTRO_ID "ubuntu")
            endif()
            
            # 提取版本号
            string(REGEX MATCH "DISTRIB_RELEASE=([^\n]*)" VERSION_MATCH "${LSB_RELEASE_CONTENT}")
            if(VERSION_MATCH)
                string(REGEX REPLACE "DISTRIB_RELEASE=([^\n]*)" "\\1" DISTRO_VERSION_ID "${VERSION_MATCH}")
            endif()
            
        # 方法4：检查 /etc/debian_version（Debian 系）
        elseif(EXISTS "/etc/debian_version")
            set(DISTRO_ID "debian")
            file(READ "/etc/debian_version" DEBIAN_VERSION)
            string(STRIP "${DEBIAN_VERSION}" DISTRO_VERSION_ID)
        endif()
        
        # 如果仍然未检测到，设置默认值
        if(DISTRO_ID STREQUAL "unknown")
            set(DISTRO_ID "centos")
            set(DISTRO_VERSION_ID "7")
            message(WARNING "无法检测 Linux 发行版，使用默认值：CentOS 7")
        endif()
        
    else()
        # 使用 CMake 3.22+ 的内置功能
        message(STATUS "使用 CMake 内置功能检测 Linux 发行版...")
        
        cmake_host_system_information(RESULT PRETTY_NAME QUERY DISTRIB_PRETTY_NAME)
        message(STATUS "系统描述: ${PRETTY_NAME}")
        
        cmake_host_system_information(RESULT DISTRO QUERY DISTRIB_INFO)
        
        # 尝试获取 DISTRIB_ID
        if(DEFINED DISTRIB_ID)
            string(TOLOWER "${DISTRIB_ID}" DISTRO_ID)
        endif()
        
        # 尝试获取版本信息
        if(DEFINED DISTRIB_VERSION_ID)
            set(DISTRO_VERSION_ID "${DISTRIB_VERSION_ID}")
        elseif(DEFINED DISTRIB_RELEASE)
            set(DISTRO_VERSION_ID "${DISTRIB_RELEASE}")
        endif()
        
        # 如果 CMake 内置方法失败，回退到文件检测
        if(NOT DEFINED DISTRO_ID)
            message(STATUS "CMake 内置检测失败，回退到文件检测方法...")
            
            if(EXISTS "/etc/os-release")
                file(READ "/etc/os-release" OS_RELEASE_CONTENT)
                string(REGEX MATCH "ID=\"?([^\"\n]*)\"?" ID_MATCH "${OS_RELEASE_CONTENT}")
                if(ID_MATCH)
                    string(REGEX REPLACE "ID=\"?([^\"]*)\"?" "\\1" DISTRO_ID "${ID_MATCH}")
                    string(TOLOWER "${DISTRO_ID}" DISTRO_ID)
                endif()
            endif()
        endif()
    endif()
    
    # 标准化发行版名称
    if(DISTRO_ID MATCHES "rhel|redhat")
        set(DISTRO_ID "centos")  # 将 RHEL 也当作 CentOS 处理
    elseif(DISTRO_ID MATCHES "debian")
        # Debian 系统可能需要特殊处理，但这里先按 Ubuntu 处理
        if(NOT DISTRO_ID STREQUAL "ubuntu")
            message(STATUS "检测到 Debian 系统，按 Ubuntu 配置处理")
        endif()
    endif()
    
    # 输出最终检测结果
    message(STATUS "=== Linux 发行版检测结果 ===")
    message(STATUS "发行版 ID: ${DISTRO_ID}")
    message(STATUS "版本号: ${DISTRO_VERSION_ID}")
    message(STATUS "==============================")
    
    # 设置 Linux 通用包含目录
    set(INCLUDE_DIR
        "/usr/local/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/LuaBind/lua"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/common/"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/mysql/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/mysql/linux"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/Theron/Include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/zdb"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/libevent/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/libevent/linux/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/zookeeper/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/common/hiredis/hiredis_linux"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/nanopb"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/OpenXLSX"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/enet/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/tirpc"
        "${CMAKE_NFSHM_SOURCE_DIR}/src"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFrame"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFServer"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFrame/NFComm/NFKernelMessage"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFrame/NFComm/NFMessageDefine"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFServer/NFServerComm/NFServerMessage"
    )
    
elseif(WIN32)
    # ========================================================================
    # Windows 平台配置
    # ========================================================================
    
    # 设置 Windows 通用包含目录
    set(INCLUDE_DIR
        "/usr/local/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/LuaBind/lua"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/common/"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/mysql/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/mysql/win"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/Theron/Include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/zdb"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/libevent/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/libevent/win/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/zookeeper/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/common/hiredis/hiredis_win"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/nanopb"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/OpenXLSX"
        "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/enet/include"
        "${CMAKE_NFSHM_SOURCE_DIR}/src"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFrame"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFServer"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFrame/NFComm/NFKernelMessage"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFrame/NFComm/NFMessageDefine"
        "${CMAKE_NFSHM_SOURCE_DIR}/src/NFServer/NFServerComm/NFServerMessage"
    )
    
    message(STATUS "✅ Windows 平台配置已应用")
endif()

# 应用包含目录
include_directories(${INCLUDE_DIR})

